/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TabelModel;

import Com.TransaksiParkirdanLaporan.Com.Transaksi.DataParkir;
import Login.Koneksi;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Tea-Nose
 */
public class DataParkir_List {

    private static Connection connection;
    private static DataParkir_List dataParkirList;

    public DataParkir_List(Connection connection) {
        //inisialisasi conn
        this.connection = connection;
    }

    public static DataParkir_List getDataParkir_List() throws SQLException, FileNotFoundException, IOException {
        //untuk mengambil status koneksi dari kelas Koneksi
        if (dataParkirList == null) {
            dataParkirList = new DataParkir_List(Koneksi.getDBConnection());
        }
        return dataParkirList;
    }

    public List<DataParkir> getDataParkir(String id_staff, String jamMsukShift) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        GregorianCalendar dateNow = new GregorianCalendar();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        String date1 = dateFormat.format(dateNow.getTime());

        connection.setAutoCommit(false);
        String sql = "select noparkir, kodehelm, jumlahhelm, nomorpolisi, tanggalmasuk, jammasuk, biayaparkir from dataparkir where idkaryawan='" + id_staff + "' and tanggalmasuk='"+date1+"' and shiftmasuk='"+jamMsukShift+"'";

        preparedStatement = connection.prepareStatement(sql);
        resultSet = preparedStatement.executeQuery();
        List<DataParkir> dataparkirz = new ArrayList<DataParkir>();

        while (resultSet.next()) {
            DataParkir dataParkir = new DataParkir();

            dataParkir.setNoParkir(resultSet.getString(1));
            dataParkir.setKode_merkHelm_jenisHelm(resultSet.getString(2));
            try {
                dataParkir.setJumlahHelm(Integer.parseInt(resultSet.getString(3)));
            } catch (Exception ex) {
                Logger.getLogger(DataParkir_List.class.getName()).log(Level.SEVERE, null, ex);
            }
            dataParkir.setNoPolisiKendaraan(resultSet.getString(4));
            dataParkir.setTanggalMasuk(resultSet.getString(5));
            dataParkir.setJamMasuk(resultSet.getString(6));
            dataParkir.setBiaya_parkir(Double.parseDouble(resultSet.getString(7)));

            dataparkirz.add(dataParkir);
        }
        connection.commit();
        return dataparkirz;
    }
}
